//
//  如遇到问题或有更好方案，请通过以下方式进行联系
//      QQ：1357127436
//      Email：kingsic@126.com
//      GitHub：https://github.com/kingsic/SGAnimation.git
//
//  SGActivePopupsView.m
//  SGAnimationExample
//
//  Created by apple on 2017/6/13.
//  Copyright © 2017年 Sorgle. All rights reserved.
//

#import "SGActivePopupsView.h"
#import "CALayer+SGAnimation.h"

#define activePopupsViewWidth [UIScreen mainScreen].bounds.size.width
#define activePopupsViewHeight [UIScreen mainScreen].bounds.size.height

@interface SGActivePopupsView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@end

@implementation SGActivePopupsView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        
        [self initialization];
        [self setupSubviews];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self initialization];
    [self setupSubviews];
}

- (void)initialization {
    _popupsTimeInterval = 0.3;
}

- (void)setupSubviews {
    [self addSubview:self.imageView];
    [self addSubview:self.cancelBtn];
    [self.imageView addSubview:self.sureBtn];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat spacing = 10;
    
    CGFloat imageViewW = activePopupsViewWidth * 3 / 4;
    CGFloat imageViewH = _imageView.image.size.height + 10;
    CGFloat imageViewX = 0.5 * (activePopupsViewWidth - imageViewW);
    CGFloat imageViewY = 0.5 * (activePopupsViewHeight - imageViewH);
    
    if (self.frame.size.width == 320) {
        imageViewW = activePopupsViewWidth * 4 / 5;
        imageViewX = 0.49 * (activePopupsViewWidth - imageViewW);
    }
    _imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    
    CGRect tempFrame = _cancelBtn.frame;
    tempFrame.origin.x = CGRectGetMaxX(_imageView.frame) - 2 * spacing;
    tempFrame.origin.y = imageViewY - 2 * spacing;
    _cancelBtn.frame = tempFrame;
    
    _sureBtn.frame = CGRectMake(15, _imageView.height - 80, _imageView.width - 30, 60);
    
    [self addAnimationWithImageView];
}

- (void)addAnimationWithImageView {
    [_imageView.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@0.1, @1.05, @1]];
    [_cancelBtn.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@0.1, @1.05, @1]];
    [_sureBtn.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@0.1, @1.05, @1]];
}

-(UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_sureBtn setTitle:@"拆开它" forState:0];
        [_sureBtn setTitleColor:RGB(148, 39, 21) forState:0];
        [_sureBtn setBackgroundColor:RGB(253, 179, 43)];
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:20.0f];
        _sureBtn.layer.masksToBounds = YES;
        _sureBtn.layer.cornerRadius = 6.0f;
        [_sureBtn addTarget:self action:@selector(sureBtn_action:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _sureBtn;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didselectImage)];
//        [_imageView addGestureRecognizer:tap];
    }
    return _imageView;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"qhb_ico_gb"] forState:(UIControlStateNormal)];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"qhb_ico_gb"] forState:(UIControlStateHighlighted)];
        [_cancelBtn addTarget:self action:@selector(cancelBtn_action) forControlEvents:(UIControlEventTouchUpInside)];
        [_cancelBtn sizeToFit];
    }
    return _cancelBtn;
}

-(void)sureBtn_action:(UIButton *)sender{
    UIColor * mycolor = [self stringTOColor:@"#ffa200"];
    [sender setBackgroundColor:mycolor];
    if (self.selectedImageBlock) {
        self.selectedImageBlock();
    }
    [self dismiss];

}

- (void)cancelBtn_action {
    [self dismiss];
}

- (void)SG_show {
    if (self.superview != nil) return;
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}

- (void)dismiss {
    [_imageView.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@1, @0.5, @0.1]];
    [_cancelBtn.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@1, @0.5, @0.1]];
    [_sureBtn.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@1, @0.5, @0.1]];
    
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.popupsTimeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf removeFromSuperview];
    });
}

#pragma mark - - - 图片的点击事件
- (void)didselectImage {
    [self removeFromSuperview];
    
    if (self.selectedImageBlock) {
        self.selectedImageBlock();
    }
}

- (void)setImageName:(UIImage *)imageName {
    _imageName = imageName;
    if (imageName) {
        _imageView.image = imageName;
    }
}

- (void)setPopupsTimeInterval:(CGFloat)popupsTimeInterval {
    _popupsTimeInterval = popupsTimeInterval;
}

- (UIColor *) stringTOColor:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        return nil;
    }
    unsigned red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 1;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
    range.location = 3;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
    range.location = 5;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
    return color;
}
@end

